//
//  M1Alert.m
//  NewMarryU
//
//  Created by apple on 2018/7/17.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1Alert.h"

@implementation M1Alert
+(void)m1_alertWithTitle:(NSString *)title message:(NSString *)message{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([[self m1_getAvailableVC] isKindOfClass:[JCAlertController class]]) {
            [[self m1_getAvailableVC] dismissViewControllerAnimated:NO completion:nil];
        }
        
        JCAlertController *m1_alert = [JCAlertController alertWithTitle:title message:message];
        JCAlertStyle *style = [JCAlertStyle shareStyle];
        style.background.canDismiss = YES;
        style.background.autoDismiss = YES;
        [[self m1_getAvailableVC] presentViewController:m1_alert animated:YES completion:nil];
    });
    
}

+(void)m1_alertWithTitle:(NSString *)title message:(NSString *)message buttonTitles:(NSArray *)titles action:(void(^)(NSInteger index))action{
    dispatch_async(dispatch_get_main_queue(), ^{
        if ([[self m1_getAvailableVC] isKindOfClass:[JCAlertController class]]) {
            [[self m1_getAvailableVC] dismissViewControllerAnimated:NO completion:nil];
        }
        if (titles.count >2) {
            return;
        }
        JCAlertStyle *style = [JCAlertStyle shareStyle];
        style.background.canDismiss = NO;
        style.background.autoDismiss = NO;
        JCAlertController *m1_alert = [JCAlertController alertWithTitle:title message:message];
        for (int i = 0; i<titles.count; i++) {
            [m1_alert addButtonWithTitle:titles[i] type:(i==titles.count-1?JCButtonTypeWarning:JCButtonTypeNormal) clicked:^{
                if (action) {
                    action(i);
                }
            }];
        }
        [[self m1_getAvailableVC] presentViewController:m1_alert animated:YES completion:nil];
    });
    
}
+(UIViewController *)m1_getAvailableVC{
    __block UIWindow *m1_indow = [[UIApplication sharedApplication].delegate window];
    if (m1_indow.windowLevel != UIWindowLevelNormal) {
        [[UIApplication sharedApplication].windows enumerateObjectsUsingBlock:^(__kindof UIWindow * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            if (obj.windowLevel == UIWindowLevelNormal) {
                m1_indow = obj;
                *stop        = YES;
            }
        }];
    }
    
    return [self m1_topVC:m1_indow.rootViewController];
}
+(UIViewController *)m1_topVC:(UIViewController *)m1_vc{
    while (m1_vc.presentedViewController) {
        m1_vc = m1_vc.presentedViewController;
    }
    if ([m1_vc isKindOfClass:[UITabBarController class]]) {
        UIViewController *sVC = [self m1_topVC:((UITabBarController *)m1_vc).selectedViewController];
        return sVC;
    } else if ([m1_vc isKindOfClass:[UINavigationController class]]) {
        UIViewController *sVC = [self m1_topVC:((UINavigationController *)m1_vc).visibleViewController];
        return sVC;
    } else {
        return m1_vc;
    }
}
@end
